﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainProject.Quanlyhanghoa
{
    public partial class Quanlyhanghoa : Form
    {
        public Quanlyhanghoa()
        {
            InitializeComponent();
            initdata();
        }

        public void initdata()
        {
            QLXT_DBDataContext data = new QLXT_DBDataContext();
            data_Hanghoa.DataSource = from q in data.HANGHOAs select q;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Themhanghoa fr = new Themhanghoa();
            fr.ShowDialog(this);
            initdata();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = data_Hanghoa.SelectedRows;

            if (rows.Count < 1)
            {
                MessageBox.Show("Hãy chọn dòng cần chỉnh sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            DataGridViewRow r = rows[0];

            Suahanghoa sua = new Suahanghoa((int)r.Cells[0].Value);
            sua.ShowDialog(this);
            initdata();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = data_Hanghoa.SelectedRows;
            QLXT_DBDataContext db = new QLXT_DBDataContext();

            if (rows.Count < 1)
            {
                MessageBox.Show("Hãy chọn dòng cần xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            foreach (DataGridViewRow r in rows)
            {
                HANGHOA khdel = db.HANGHOAs.Single(p => p.IDHANGHOA == (int)r.Cells[0].Value);
                db.HANGHOAs.DeleteOnSubmit(khdel);
            }

            db.SubmitChanges();

            initdata();
        }
    }
}
